package su.msk.jet.team01.chatik.server.data;

import javax.naming.InvalidNameException;

import su.msk.jet.team01.chatik.server.exception.NameInUseException;

public class Client {

	private Feedback feedback;
	private Room room;
	private String name = "";

	public Client(final Feedback feedback) {
		RoomManager roomManager = DataManager.getRoomManager();

		this.feedback = feedback;

		room = roomManager.getDefaultRoom();
		room.addClient(this);
	}

	public void changeName(final String newName) throws InvalidNameException,
			NameInUseException {
		ClientManager clientManager = DataManager.getClientManager();

		clientManager.changeClientName(this, newName);
	}

	public void changeRoom(final Room newRoom) {
		room.removeClient(this);
		newRoom.addClient(this);
		room = newRoom;
	}

	public void disconnect() {
		feedback.disconnect();
	}
	
	public void remove() {
		ClientManager clientManager = DataManager.getClientManager();
		clientManager.removeClient(this);
		room.removeClient(this);
	}

	public String getName() {
		return name;
	}

	public Room getRoom() {
		return room;
	}

	public void send(final String msg, final Object... args) {
		feedback.sendMessage(String.format(msg, args));
	}

	@Override
	public String toString() {
		return name;
	}

	protected void setName(final String newName) {
		name = newName;
	}
}
